require.config({
    paths: {
        "common": "common/common",
        'pagin': "common/pagin"
    }
    // baseUrl: 'js/common'
});

require(['common', 'pagin'], function(defaultStr, pagin) {
    var baseUrl = defaultStr();
    /*----------
     *ajax数据请求
     *部门页面管理————补助页面
     *部门页面管理————补发页面
     *默认打开该页的时候就已经知道了是那个部门
     *并且只显示该部门的信息
     */

    var shenhe_table = new Vue({
        el: '#main-data',
        data: {
            items: [],
            status: [{
                statusCode: 0,
                statusName: '未提交'
            }, {
                statusCode: 1,
                statusName: '已提交待审核'
            }, {
                statusCode: 2,
                statusName: '审核通过'
            }, {
                statusCode: 3,
                statusName: '审核未通过'
            }],
            query: {
                "startPage": 0,
                "pageNum": 10,
                "statusCode": 0,
                "helpDate": '',
                "deptId": ''
            },
            dates: '',
            check_id:[],
            depts: [],
            /*pages:用于构造分页的数组*/
            pages: [],
            totalPage: 1,
            /*cur_page 当前页*/
            curPage: 1,
            url: {
                'queryBuZhu': baseUrl + "/usr/buzhu/getBuzhuByPage.do",
                'searchBuzhu': baseUrl + "/usr/buzhu/searchBuzhuByPage.do",
                'getMonths': baseUrl + '/usr/date/getAll.do',
                'getDepts': baseUrl + '/base/dept/list.do',
                'exportDataWord': baseUrl + '/jasper/zgWorddownload.do',
                'exportDataExcel': baseUrl + '/jasper/zgExceldownload.do'
            }
        },
        methods: {
            /*初始化页面*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryMonth();
                _self.queryDepts();
            },
            /*对部门的查询*/
            queryDepts: function() {
                var _self = this;
                var data = {
                    "rows": 10,
                    "page": 1,
                };
                $.ajax({
                    type: "POST",
                    url: _self.url.getDepts,
                    data: JSON.stringify(data),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        _self.depts = [];
                        console.log(result);
                        $.each(result.result, function(i, value) {
                            _self.depts.push(value);
                        });
                    }
                });
            },
            /*对日期的查询*/
            queryMonth: function() {
                var _self = this;
                _self.dates = [];
                $.ajax({
                    type: "GET",
                    url: _self.url.getMonths,
                    dataType: "json",
                    success: function(result) {
                        $.each(result.body, function(i, value) {
                            if (parseInt(value.helpMonth) > 10) {
                                var temp = value.helpYear + '' + value.helpMonth
                            } else {
                                var temp = value.helpYear + '0' + value.helpMonth

                            }
                            _self.dates.push(temp);
                        })
                    }
                })
            },
            /*用户提交查询信息，查询学生信息*/
            queryData: function(str) {
                var _self = this;
                if (str == "all") {
                    _self.query.startPage = 0;
                }
                _self.items = [];
                /*提交数据到服务器 query是要查询的数据对象*/
                console.log(_self.query.helpDate);
                console.log(_self.query.deptId);

                if (_self.query.statusCode === 4) {
                    console.log('_self.query.statusCode');
                    var url = _self.url.queryBuZhu + '?' + 'startPage=' + _self.query.startPage + '&pageNum=' + _self.query.pageNum;
                } else if (_self.query.helpDate && _self.query.deptId) {
                    // var url = _self.url.searchBuzhu + '?' + 'startPage=' + _self.query.startPage + '&pageNum=' + _self.query.pageNum + '&statusCode=' + _self.query.statusCode + '&deptId=' + _self.query.deptId + '&helpDate=' + _self.query.helpDate;
                    var url = _self.url.searchBuzhu;
                    var data={
                        "deptId":_self.query.deptId,
                        "helpDate":_self.query.helpDate,
                        "currentPage":_self.query.startPage,
                        "pageSize":_self.query.pageNum
                    }    
                
                } else if (_self.query.helpDate == "" && _self.query.deptId == "") {
                    
                    var url = _self.url.queryBuZhu + '?' + 'startPage=' + _self.query.startPage + '&pageNum=' + _self.query.pageNum + '&statusCode=' + _self.query.statusCode;

                } else {
                    
                    var url = _self.url.searchBuzhu;
                    var data={
                        "deptId":_self.query.deptId,
                        "helpDate":_self.query.helpDate,
                        "currentPage":_self.query.startPage,
                        "pageSize":_self.query.pageNum
                    }
                    // if (_self.query.helpDate == "") {
                    //     var url = _self.url.searchBuzhu + '?' + 'startPage=' + _self.query.startPage + '&pageNum=' + _self.query.pageNum + '&statusCode=' + _self.query.statusCode + '&deptId=' + _self.query.deptId;
                    // } else if (_self.query.deptId == "") {
                    //     var url = _self.url.searchBuzhu + '?' + 'startPage=' + _self.query.startPage + '&pageNum=' + _self.query.pageNum + '&statusCode=' + _self.query.statusCode + '&helpDate=' + _self.query.helpDate;
                    // }
                }

                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data:JSON.stringify(data),
                    success: function(result) {
                        $.each(result.body.result, function(i, val) {
                            _self.items.push(val)
                        });
                        if (result.body.total > 10) {
                            /*如果总数大于10就分页*/
                            /*_self.totalPage 总页数*/
                            _self.totalPage = Math.ceil(result.body.total / _self.query.pageNum);
                            var result = pagin.paginGetli(_self.totalPage, _self.curPage);
                            _self.pages = result.pages;
                        } else {
                            _self.pages = [];
                        }
                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                console.log(cur_page);
                _self.query.startPage = cur_page - 1;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理助管列表分页查询*/
            submitPage: function(p) {
                var _self = this;
                console.log(p);
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            exportData: function(str) {
                var _self = this;
                if (str == "word") {
                    if (_self.query.helpDate == undefined && _self.query.deptId == undefined) {
                        var url = _self.url.exportDataWord + '?' + 'helpDate=' + _self.query.helpDate + '&statusCode=' + _self.query.statusCode + '&deptId=' + _self.query.deptId;
                    }
                    var url = _self.url.exportDataWord + '?' + 'helpDate=' + _self.query.helpDate + '&statusCode=' + _self.query.statusCode + '&deptId=' + _self.query.deptId;

                } else {
                    var url = _self.url.exportDataExcel + '?' + 'helpDate=' + _self.query.helpDate + '&statusCode=' + _self.query.statusCode + '&deptId=' + _self.query.deptId;

                }
                $.ajax({
                    type: "GET",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(result) {

                    },
                    error: function(message) {
                        alert("提交数据失败！");
                    }
                });
                // window.location.replace('http://localhost:8080/stu_assist_management/jasper/zgWorddownload.do?helpDate=201610&statusCode=2&deptId=006')
            }
        }
    });
    shenhe_table.init();
});

function getWordUrl(test) {
    var status = $("select[name='status'] option:selected").val();
    alert(status);

    var ym = $("select[name='yearMonth'] option:selected").val();
    alert(ym);

    var deptId = $("select[name='dept'] option:selected").val();
    alert(deptId);

    var url = "http://115.29.136.190:8080/help/jasper/zgWorddownload.do?helpDate=" + ym + "&statusCode=" + status + "&deptId=" + deptId;

    test.href = url;
}

function getExcelUrl(test) {
    var status = $("select[name='status'] option:selected").val();
    alert(status);

    var ym = $("select[name='yearMonth'] option:selected").val();
    alert(ym);

    var deptId = $("select[name='dept'] option:selected").val();
    alert(deptId);

    var url = "http://115.29.136.190:8080/help/jasper/zgExceldownload.do?helpDate=" + ym + "&statusCode=" + status + "&deptId=" + deptId;

    test.href = url;
}
